#include <stdio.h>
#include <string.h>
#define maxn 110

char dna[maxn * 2], mindna[maxn], subdna[maxn];

int main() {
  int k, i, j, len, is_smaller, lexipos;
  scanf("%d", &k);
  while (k--) {
    memset(dna, '\0', sizeof(dna));
    memset(mindna, '\0', sizeof(mindna));
    memset(subdna, '\0', sizeof(subdna));
    scanf("%s", dna);
    len = strlen(dna);
    strncat(mindna, dna, len);    
    strncat(dna, dna, len);
    lexipos = 0;
    for (i = 0; i < len; i++) {
      for (j = 0; j < len; j++) {
        subdna[j] = dna[j + i];
      }
      
      is_smaller = 1;
      for (j = 0; j <= lexipos; j++) {
        if (subdna[j] > mindna[j]) {
          is_smaller = 0;
          break;
        }
      }
      if (is_smaller) {
        strncpy(mindna, subdna, len);
        lexipos++;
      }
    }
    
    puts(mindna);
  }
  
  return 0;
}
